//package com.disease.api.service.utils;
//
//import org.apache.commons.lang3.ArrayUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import javax.crypto.Cipher;
//import java.io.ByteArrayOutputStream;
//import java.nio.charset.StandardCharsets;
//import java.security.*;
//import java.security.interfaces.RSAPrivateKey;
//import java.security.interfaces.RSAPublicKey;
//import java.security.spec.InvalidKeySpecException;
//import java.security.spec.PKCS8EncodedKeySpec;
//import java.security.spec.X509EncodedKeySpec;
//import java.util.Base64;
//import java.util.HashMap;
//import java.util.Map;
//
//public class RSAUtil {
//    private static final Logger log = LoggerFactory.getLogger(RSAUtil.class);
//    public static final String KEY_ALGORITHM = "RSA";
//    private static final String PUBLIC_KEY = "RSAPublicKey";
//    private static final String PRIVATE_KEY = "RSAPrivateKey";
//    private static final String CHARSET = "utf-8";
//    private static final int KEYSIZE = 2048;
//    private static final int RESERVE_BYTES = 11;
//    private static final String ECB_PADDING = "RSA/ECB/PKCS1Padding";
//    private static final int MAX_DECRYPT_BLOCK = 256;
//    private static final int MAX_ENCRYPT_BLOCK = 245;
//
//    public RSAUtil() {
//    }
//
//    private static Map<String, Object> initKey(int keysize) throws Exception {
//        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
//        keyPairGen.initialize(keysize);
//        KeyPair keyPair = keyPairGen.generateKeyPair();
//        RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic();
//        RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate();
//        Map<String, Object> keyMap = new HashMap(2);
//        keyMap.put("RSAPublicKey", publicKey);
//        keyMap.put("RSAPrivateKey", privateKey);
//        return keyMap;
//    }
//
//    private static String getPublicKeyStr(Map<String, Object> keyMap) {
//        Key key = (Key)keyMap.get("RSAPublicKey");
//        return encryptBASE64(key.getEncoded());
//    }
//
//    private static String getPrivateKeyStr(Map<String, Object> keyMap) {
//        Key key = (Key)keyMap.get("RSAPrivateKey");
//        return encryptBASE64(key.getEncoded());
//    }
//
//    private static PublicKey getPublicKey(String publicKeyString) throws NoSuchAlgorithmException, InvalidKeySpecException {
//        byte[] publicKeyByte = Base64.getDecoder().decode(publicKeyString);
//        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyByte);
//        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//        return keyFactory.generatePublic(keySpec);
//    }
//
//    private static PrivateKey getPrivateKey(String privateKeyString) throws Exception {
//        byte[] privateKeyByte = Base64.getDecoder().decode(privateKeyString);
//        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyByte);
//        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//        return keyFactory.generatePrivate(keySpec);
//    }
//
//    public static String encryptBASE64(byte[] key) {
//        return new String(Base64.getEncoder().encode(key));
//    }
//
//    public static byte[] decryptBASE64(String key) {
//        return Base64.getDecoder().decode(key);
//    }
//
//    public static String encrypt(String text, String publicKeyStr) {
//        try {
//            Cipher cipher = Cipher.getInstance("RSA");
//            cipher.init(1, getPublicKey(publicKeyStr));
//            byte[] data = text.getBytes(StandardCharsets.UTF_8);
//            byte[] enBytes = null;
//
//            for(int i = 0; i < data.length; i += 245) {
//                byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(data, i, i + 245));
//                enBytes = ArrayUtils.addAll(enBytes, doFinal);
//            }
//
//            return Base64.getEncoder().encodeToString(enBytes);
//        } catch (Exception e) {
//            throw new RuntimeException("加密字符串[" + text + "]时遇到异常", e);
//        }
//    }
//
//    public static String decrypt(String secretText, String privateKeyStr) {
//        if (StringUtils.isBlank(secretText)) {
//            return "";
//        } else {
//            try {
//                Cipher cipher = Cipher.getInstance("RSA");
//                cipher.init(2, getPrivateKey(privateKeyStr));
//                byte[] data = Base64.getDecoder().decode(secretText);
//                int inputLen = data.length;
//                ByteArrayOutputStream out = new ByteArrayOutputStream();
//                int offSet = 0;
//
//                for(int i = 0; inputLen - offSet > 0; offSet = i * 256) {
//                    byte[] cache;
//                    if (inputLen - offSet > 256) {
//                        cache = cipher.doFinal(data, offSet, 256);
//                    } else {
//                        cache = cipher.doFinal(data, offSet, inputLen - offSet);
//                    }
//
//                    out.write(cache, 0, cache.length);
//                    ++i;
//                }
//
//                out.close();
//                return out.toString("utf-8");
//            } catch (Exception e) {
//                throw new RuntimeException("解密字符串[" + secretText + "]时遇到异常", e);
//            }
//        }
//    }
//
//    private static String encryptData(String text, String privateInfoStr) {
//        try {
//            log.info("明文字符串为:[{}]", text);
//            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
//            cipher.init(1, getPrivateKey(privateInfoStr));
//            byte[] data = text.getBytes(StandardCharsets.UTF_8);
//            byte[] enBytes = null;
//
//            for(int i = 0; i < data.length; i += 245) {
//                byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(data, i, i + 245));
//                enBytes = ArrayUtils.addAll(enBytes, doFinal);
//            }
//
//            return Base64.getEncoder().encodeToString(enBytes);
//        } catch (Exception e) {
//            throw new RuntimeException("加密字符串[" + text + "]时遇到异常", e);
//        }
//    }
//
//    private static String decryptData(String secretText, String publicInfoStr) {
//        try {
//            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
//            cipher.init(2, getPublicKey(publicInfoStr));
//            byte[] data = Base64.getDecoder().decode(secretText);
//            int inputLen = data.length;
//            ByteArrayOutputStream out = new ByteArrayOutputStream();
//            int offSet = 0;
//
//            for(int i = 0; inputLen - offSet > 0; offSet = i * 256) {
//                byte[] cache;
//                if (inputLen - offSet > 256) {
//                    cache = cipher.doFinal(data, offSet, 256);
//                } else {
//                    cache = cipher.doFinal(data, offSet, inputLen - offSet);
//                }
//
//                out.write(cache, 0, cache.length);
//                ++i;
//            }
//
//            out.close();
//            return out.toString("utf-8");
//        } catch (Exception e) {
//            throw new RuntimeException("解密字符串[" + secretText + "]时遇到异常", e);
//        }
//    }
//
//    public static void main(String[] args) throws Exception {
//        //授权码
//        String content = "2024@abc!pkj#";
//        String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjdcHczy6PQL7kkG99E+xc6BM3Bs6bPeL9pYhjlHZWC5Ylhqz9/WkGgE6UPfQkovou9LCN3l2gVdyhHUbZ3KMxE3inSFdJBioCrmFa+QjjwRy11KvXTHRsyG2Q6l29j9tLJBCU41g7RPfVRjkWiedp1NddYWztCSUq7KmtKQ+Z81T0Vz9/5BGGHHkux8Y55PGvMZ/vOOCMqJ1msKlsDPEUljucln9JSbs2ajM3juFHpyVAp4Lv0E6MUZf51eaBVVWSuLgZX0OakNPi3pIaPkRd5HbRhb87jik3iWBpmkrMYyWqD4v4o87MjoZkBq5gOl8S50Ii5TtyH9EHbLJ02xJ7wIDAQAB";
//        System.out.println("公钥:" + publicKey);
//        String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCN1wdzPLo9AvuSQb30T7FzoEzcGzps94v2liGOUdlYLliWGrP39aQaATpQ99CSi+i70sI3eXaBV3KEdRtncozETeKdIV0kGKgKuYVr5COPBHLXUq9dMdGzIbZDqXb2P20skEJTjWDtE99VGORaJ52nU111hbO0JJSrsqa0pD5nzVPRXP3/kEYYceS7Hxjnk8a8xn+844IyonWawqWwM8RSWO5yWf0lJuzZqMzeO4UenJUCngu/QToxRl/nV5oFVVZK4uBlfQ5qQ0+Lekho+RF3kdtGFvzuOKTeJYGmaSsxjJaoPi/ijzsyOhmQGrmA6XxLnQiLlO3If0QdssnTbEnvAgMBAAECggEACSM3KadZ4CBXEJ8Hm7uelPJYwT1H0ElKTQJy+vYu6E1kC5eAzJKYlcxKc3jjiO1WevMi6+v5RG/bwsjAkTOB+guCc9j4ZSDpgnddLwwtdkM1GsYK4hgZiE69rTI3pbmEUQCiJTvQaLWFB7hozZ4LvAHU3YNVhl9s7Hq/3pdOJeWt0uqfZ0SlJUKsX/8q8iw+IdsU+XXZ95ivg0gckIkaqLhS4VjjsUANXAhKrrOcKcGRpI/FCRQ8KOWuHvEegf9NPVPIymLpUEAEqEMBLf9GgEvslWfOKxj213wIciUMGlDF6f2TWV12Z14XzQesiTRFW4qPAZzSbi/9WBcn682IoQKBgQDHXYARxCm7SgwMa+NjMxA2Ky+5Jsfx9MTQU5ooV4TQExiK2fWHT7Y3lcWJzY6vydJdkL0Si+EkHkeT2lBtYUw0BDlrzCETo49mqPV4/9Z6Tf5ZdJ+RvFSHkiep0zMFNenq3ZELIdYhAeThlwbHrfoA928P2ClfSXlqBsuQoS0tCQKBgQC2Ihmo0BL2YMf5YB4QF0bKSXsye736YtA9HAJMjowTya6GqYFhc7ZmrWnT7Ai0BUDayXiVtUybpQN3ANNs95oY5m7gCJTfipf2cza/GY4BTJjpLjbO0t5MFGWnbX9CRwMRAWZcuOmfxwpyHT+Bt7jgW5UEdCC7D+ocDiQ4py71NwKBgQC3rjiZpc8BJKbXJuu4cMqF7eIpR0a+cNKIUg5dDs1p274n58qkYVez/MSkQwQsh9jOpdTdao0C9vbhjSm+rP9FiYLSA5+R9YbggdiwI56azpMCHkYKQqbqxUjP2atPKK4c2wiQZhtgC9ux0LDJdkw5ab8krPbpOMupWRtiZG036QKBgQCM8nTDBEs4aYs3/F8neqvozdbOKZ7Kjv+x2uQYlNPoWFJ8md6rKN4GNbtJKkdN8SbQacusp4IijR/ST0xO7opNLL6vF1UGbX3OVjkOyFrxvHI1OfNmOR+Y5swINp+eDdBcr6WkqeD6KhF2YBYOb7xdu+HlcW0G9njovzTettqLHwKBgQCIN3Gjm/PqCO7yE44WGORpAHZ+iv3qySjKbnVKGpDGHDNAxLC3DsNcj23pEl7YH7jjD76N789qG0F+EAjlvO+/0jWb3aSLupIKyxZoRQ9aWAiEsREzc9Sczar46gr6x/WpEN+TWNX1zSKBhuZPLFwJQzPyNh/6tJJCrSKJMgdxvw==";
//        System.out.println("私钥:" + privateKey);
//        String cipherText = encrypt(content, publicKey);
//        System.out.println("加密后的密文:" + cipherText);
//        String plainText = decrypt(cipherText, privateKey);
//        System.out.println("解密后明文:" + plainText);
//    }
//}
